package com.le.tester.onjava8.l_2_2_7;

import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * createTime：2023/1/10 16:00
 * description：深拷贝ArrayList
 */

class Int2 implements Cloneable {
    private int i;

    Int2(int i) {
        this.i = i;
    }

    public void increment() {
        i++;
    }

    @Override
    public String toString() {
        return Integer.toString(i);
    }

    @Override
    protected Int2 clone() {
        try {
            return (Int2) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}

class Int3 extends Int2 {
    private int j;

    Int3(int i) {
        super(i);
    }
}


public class AddingClone {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Int2 x = new Int2(10);
        Int2 x2 = x.clone();
        x2.increment();

        System.out.println("x= " + x + ",x2=" + x2);

        Int3 x3 = new Int3(7);

        x3 = (Int3) x3.clone();

        ArrayList<Int2> v = IntStream.range(0, 10)
                .mapToObj(Int2::new)
                .collect(Collectors.toCollection(ArrayList::new));

        System.out.println("v: " + v);

        ArrayList<Int2> v2 = (ArrayList) v.clone();

        IntStream.range(0, v.size()).forEach(i -> v2.set(i, v.get(i).clone()));

//
        v2.forEach(Int2::increment);
        System.out.println("v2: " + v2);

        System.out.println("v: " + v);
    }
}
